﻿using System;
using TY.UC.Common;
using TY.UT.Pay.WxPay.lib;

namespace TY.UT.Pay.WxPay
{
    public class RefundHelper
    {
        /// <summary>
        /// 微信退款
        /// </summary>
        /// <param name="transactionId">微信交易凭证</param>
        /// <param name="outTradeNo">用户订单号码</param>
        /// <param name="totalFee">订单总金额(元)</param>
        /// <param name="refundFee">退款金额(元)</param>
        /// <returns></returns>
        public static bool Run(string transactionId, string outTradeNo, float totalFee, float refundFee)
        {
            LogHelper.WriteLog("RefundHelper-Run，微信退款金额记录：[transactionId]=" + transactionId + "[outTradeNo]" +
                               outTradeNo + "[total_fee]=" + totalFee + "[refundFee]=" + refundFee);
            WxPayData data = new WxPayData();
            if (!string.IsNullOrEmpty(transactionId))//微信订单号存在的条件下，则已微信订单号为准
            {
                data.SetValue("transaction_id", transactionId);
            }
            else//微信订单号不存在，才根据商户订单号去退款
            {
                data.SetValue("out_trade_no", outTradeNo);
            }
            data.SetValue("total_fee", Convert.ToInt32(totalFee * 100));//订单总金额
            data.SetValue("refund_fee", Convert.ToInt32(refundFee * 100));//退款金额
            data.SetValue("out_refund_no", WxPayApi.GenerateOutTradeNo());//随机生成商户退款单号
            data.SetValue("op_user_id", WxPayConfig.Mchid);//操作员，默认为商户号

            //string path = HttpContext.Current.Request.PhysicalApplicationPath;
            //X509Certificate2 cert = new X509Certificate2(path + WxPayConfig.SSLCERT_PATH, WxPayConfig.SSLCERT_PASSWORD);
            WxPayData result = WxPayApi.Refund(data);//提交退款申请给API，接收返回数据

            LogHelper.WriteLog("RefundHelper-Run，微信退款返回结果" + result.ToJson());

            return result.IsSet("return_code") && result.IsSet("result_code") &&
                   result.GetValue("result_code").ToString().ToLower() == "success";
        }
        /// <summary>
        /// APP支付的微信退款
        /// </summary>
        /// <param name="transactionId">微信交易凭证</param>
        /// <param name="outTradeNo">用户订单号码</param>
        /// <param name="totalFee">订单总金额(元)</param>
        /// <param name="refundFee">退款金额(元)</param>
        /// <returns></returns>
        public static bool APPRun(string transactionId, string outTradeNo, float totalFee, float refundFee)
        {
            LogHelper.WriteLog("RefundHelper-APPRun，微信退款金额记录：[transactionId]=" + transactionId + "[outTradeNo]" +
                               outTradeNo + "[total_fee]=" + totalFee + "[refundFee]=" + refundFee);
            WxPayData data = new WxPayData();
            if (!string.IsNullOrEmpty(transactionId))//微信订单号存在的条件下，则已微信订单号为准
            {
                data.SetValue("transaction_id", transactionId);
            }
            else//微信订单号不存在，才根据商户订单号去退款
            {
                data.SetValue("out_trade_no", outTradeNo);
            }
            data.SetValue("total_fee", Convert.ToInt32(totalFee * 100));//订单总金额
            data.SetValue("refund_fee", Convert.ToInt32(refundFee * 100));//退款金额
            data.SetValue("out_refund_no", WxPayApi.GenerateOutTradeNo());//随机生成商户退款单号
            data.SetValue("op_user_id", WxPayConfig.SHMchid);//操作员，默认为商户号

            WxPayData result = WxPayApi.Refund(data, true);//提交退款申请给API，接收返回数据

            LogHelper.WriteLog("RefundHelper-APPRun，微信退款返回结果" + result.ToJson());

            return result.IsSet("return_code") && result.IsSet("result_code") &&
                   result.GetValue("result_code").ToString().ToLower() == "success";
        }
    }
}